package com.plian.system.controller.transfer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.Tools.CollectionUtil;
import com.plian.system.config.AppKeySign;
import com.plian.system.dto.transfer.DataPlatSign;
import com.plian.system.dto.transfer.IndicatorSign;
import com.plian.system.entity.fr.bank.BankKey;
import com.plian.system.entity.im.FundProject;
import com.plian.system.entity.im.FundProjectExecution;
import com.plian.system.entity.im.FundProjectInfo;
import com.plian.system.entity.sys.Org;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.service.fr.bank.IBankKeyService;
import com.plian.system.service.im.IFundProjectExecutionService;
import com.plian.system.service.im.IFundProjectInfoService;
import com.plian.system.service.im.IFundProjectService;
import com.plian.system.service.sys.orgzation.IOrgService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO 基金项目 - 输出
 * @Date: 2022/11/8 5:04 PM
 * @Version: 1.0
 **/
@RestController
@RequestMapping("api/fundProjectTransfer")
@AllArgsConstructor
@Api(value = "基金项目-输出", tags = "基金项目输出")
@Slf4j
public class FundProjectTransferController {

    private IBankKeyService bankKeyService;

    private IOrgService orgService;

    private IFundProjectInfoService fundProjectInfoService;

    private IFundProjectExecutionService fundProjectExecutionService;

    private IFundProjectService fundProjectService;
    /**
     * 提供时间段内的基金项目基本信息
     * @param dataPlatSign
     */
    @PostMapping(value = "/fun-project-time")
    @PassToken
    @AppKeySign
    public R<List<FundProjectInfo>> getFunProjectByTime(@RequestBody DataPlatSign dataPlatSign){
        log.debug("toklen:" + dataPlatSign.getToken());
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        List<Org> allChild = orgService.getAllChild(topGroupId);
        List<String> orgIds = allChild.stream().map(Org::getId).collect(Collectors.toList());
        List<FundProjectInfo> list = fundProjectInfoService.list(new LambdaQueryWrapper<FundProjectInfo>().in(FundProjectInfo::getCreateCompanyId, orgIds));
        if (Optional.ofNullable(dataPlatSign.getEndTime()).isPresent()){
            list = list.stream().filter(project -> Optional.ofNullable(project.getUpdateTime()).isPresent() &&
                    (dataPlatSign.getStartTime().compareTo(project.getUpdateTime()) < 1 && dataPlatSign.getEndTime().compareTo(project.getUpdateTime()) > -1)).collect(Collectors.toList());
        }
        return R.data(list);
    }

    /**
     * 提供时间段内的 基金项目投资计划执行 信息
     * @param indicatorSign
     */
    @PostMapping(value = "/fun-project-execution")
    @PassToken
    @AppKeySign
    public R<List<FundProjectExecution>> getFundProjectExecutionByTime(@RequestBody IndicatorSign indicatorSign){
        log.debug("toklen:" + indicatorSign.getToken());
        String appId = indicatorSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        List<Org> allChild = orgService.getAllChild(topGroupId);
        List<String> orgIds = allChild.stream().map(Org::getId).collect(Collectors.toList());
        LambdaQueryWrapper<FundProjectExecution> queryWrapper = new LambdaQueryWrapper<FundProjectExecution>().in(FundProjectExecution::getCreateCompanyId, orgIds)
                .eq(FundProjectExecution::getStatus, 3);
        if (Optional.ofNullable(indicatorSign.getMonth()).isPresent()){
            String month = indicatorSign.getYear() + "-" + indicatorSign.getMonth();
            queryWrapper.eq(FundProjectExecution::getFilingMonth, month);
        }
        List<FundProjectExecution> list = fundProjectExecutionService.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(list)){
            List<Long> baseIds = list.stream().map(FundProjectExecution::getId).collect(Collectors.toList());
            List<FundProject> fundProjects = fundProjectService.list(new LambdaQueryWrapper<FundProject>().in(FundProject::getFundProjectExecutionId,baseIds));
            if (CollectionUtil.isNotEmpty(fundProjects)){
                Map<Long, List<FundProject>> map = fundProjects.stream().collect(Collectors.groupingBy(FundProject::getFundProjectExecutionId));
                list.forEach(execution ->{
                    execution.setFundProjects(map.get(execution.getId()));
                });
            }
        }

        return R.data(list);
    }




}
